package com.tianya.portal.blog.service.impl;

import com.tianya.common.web.utils.StringUtils;
import com.tianya.portal.blog.domain.BlogNewsBook;
import com.tianya.portal.blog.mapper.BlogNewsBookMapper;
import com.tianya.portal.blog.service.IBlogNewsBookService;
import com.tianya.portal.system.domain.SysFile;
import com.tianya.portal.system.mapper.SysFileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 日常随笔Service业务层处理
 *
 * @author tianya
 * @date 2023-05-10
 */
@Service
public class BlogNewsBookServiceImpl implements IBlogNewsBookService {
    @Autowired
    private BlogNewsBookMapper blogNewsBookMapper;

    @Autowired
    private SysFileMapper sysFileMapper;

    /**
     * 查询日常随笔
     *
     * @param id 日常随笔主键
     * @return 日常随笔
     */
    @Override
    public BlogNewsBook selectBlogNewsBookById(String id) {
        blogNewsBookMapper.updateViewNumber(id);
        BlogNewsBook newsBook = blogNewsBookMapper.selectBlogNewsBookById(id);
        if (StringUtils.isNotEmpty(newsBook.getImageId())) {
            SysFile sysFile = sysFileMapper.selectSysFileById(newsBook.getImageId());
            newsBook.setSysFile(sysFile);
        }
        return newsBook;
    }

    /**
     * 查询日常随笔列表
     *
     * @param blogNewsBook 日常随笔
     * @return 日常随笔
     */
    @Override
    public List<BlogNewsBook> selectBlogNewsBookList(BlogNewsBook blogNewsBook) {
        List<BlogNewsBook> books = blogNewsBookMapper.selectBlogNewsBookList(blogNewsBook);
        for (BlogNewsBook book : books) {
            if (StringUtils.isNotEmpty(book.getImageId())) {
                SysFile sysFile = sysFileMapper.selectSysFileById(book.getImageId());
                book.setSysFile(sysFile);
            }
        }
        return books;
    }
}
